//
//  DDWoInfoVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/7/28.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDWoInfoVC.h"
#import "UIImageView+WebCache.h"
#import "PECropViewController.H"

//#import "LXActionSheet.h"
//#import "ZYQAssetPickerController.h"
#import "ImageFileInfo.h"
#import "UIImage+External.h"
#import "WoEditVC.h"
#import "DDUpdatePasswordVC.h"
#import "DDNetWorkAPIClient.h"
#import "DDSexVC.h"
#import "DDAddressVC.h"
@interface DDWoInfoVC ()<PECropViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
{
     NSMutableArray * array_picker;
}
@property(nonatomic,strong)NSArray *datas;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)UIImageView *headerImageView;
@property(nonatomic,strong)ImageFileInfo *imageInfo;

@property(nonatomic,strong) UIImage * paramImage_process;
@end

@implementation DDWoInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"个人信息", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0.01)];
}

- (void)createData
{
    
    
    DDSharedUser *user = [DDSharedUser shared];
    DDUser *userinfo = user.userInfo;
    NSMutableArray *group1 = [NSMutableArray array];
    [group1 addObject:@{@"头像":userinfo.avatarUrl==nil?@"":userinfo.avatarUrl}];
    [group1 addObject:@{@"昵称":userinfo.nickName==nil?@"":userinfo.nickName}];
    NSString *gender;
    if ([userinfo.gender isEqualToString:@"0"]) {
        gender = @"男";
    }else{
        gender = @"女";
    }
    [group1 addObject:@{@"性别":gender}];
    [group1 addObject:@{@"学校":userinfo.school==nil?@"":userinfo.school}];
    [group1 addObject:@{@"年级":userinfo.grade==nil?@"":userinfo.grade}];
    [group1 addObject:@{@"班级":userinfo.clzss==nil?@"":userinfo.clzss}];
    [group1 addObject:@{@"地址管理":@""}];
    [group1 addObject:@{@"丁当段位":userinfo.rank==nil?@"":userinfo.rank}];
    [group1 addObject:@{@"丁当号":userinfo.no==nil?@"":userinfo.no}];
    
    NSMutableArray *group2 = [NSMutableArray array];
    //[group2 addObject:@{@"绑定手机":userinfo.phoneNumber==nil?@"":userinfo.phoneNumber}];
    [group2 addObject:@{@"修改密码":@""}];
    
    _datas = @[group1,group2];
}


#pragma mark -UITableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_datas[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _datas.count;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    NSArray *group = _datas[indexPath.section];
    NSDictionary *dict = group[indexPath.row];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 80, 22)];
    label.text =dict.allKeys[0];
    label.textColor = [UIColor lightGrayColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont fontWithName:@"Arial" size:18.0];
    [cell.contentView addSubview:label];
    if (indexPath.row == 0 && indexPath.section == 0 ) {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width-70, 2, 40, 40)];
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:dict.allValues[0]] placeholderImage:[UIImage imageNamed:@"首页列表头像-通用"]options:SDWebImageRefreshCached];
        _headerImageView.layer.cornerRadius = 20.0;
        _headerImageView.layer.masksToBounds = YES;
        [cell.contentView addSubview:_headerImageView];
    }else{
        UILabel *valuelabel = [[UILabel alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width-150, 7, 120, 30)];
        valuelabel.text =dict.allValues[0];
        valuelabel.textColor = [UIColor lightGrayColor];
        valuelabel.textAlignment = NSTextAlignmentRight;
        valuelabel.font = [UIFont fontWithName:@"Arial" size:18.0];
        [cell.contentView addSubview:valuelabel];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    DDSharedUser *user = [DDSharedUser shared];
    DDUser *userinfo = user.userInfo;
    
    if (indexPath.row == 0 && indexPath.section == 0) {
//        LXActionSheet *sheet = [[LXActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"立即拍照" otherButtonTitles:@"相册选择",nil];
//        sheet.tag = 12;
//        [sheet setdestructiveButtonColor:RGB(86, 170, 14) titleColor:[UIColor whiteColor] icon:[UIImage imageNamed:@"micon-camera"]];
//        [sheet setCancelButtonColor:[UIColor whiteColor] titleColor:[UIColor redColor] icon:nil];
//        [sheet setButtonIndex:1 buttonColor:[UIColor whiteColor] titleColor:[UIColor darkTextColor] icon:[UIImage imageNamed:@"micon-album"]];
//        [sheet showInView:self.view];
        
        UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册选择",@"立即拍照", nil];
        [actionSheet showInView:self.view];
    }
    else if (indexPath.row == 1 && indexPath.section == 0)
    {
        [self edit:indexPath withEditTitle:@"昵称" withEditValue:userinfo.nickName withPlaceholder:@""];
    }
    else if (indexPath.row == 2 && indexPath.section == 0)
    {
        __weak DDWoInfoVC *weakSelf = self;
        DDSexVC *sexVC = [[DDSexVC alloc] initWithNibName:@"DDSexVC" bundle:nil];
        sexVC.resultBlock = ^(NSString *result){
            
            DDSharedUser *user = [DDSharedUser shared];
            DDUser *userinfo = user.userInfo;
            userinfo.gender = result;
            
            DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
            [network userEdit:[weakSelf validateString:userinfo.userName] nickName:[weakSelf validateString:userinfo.nickName] name:[weakSelf validateString:userinfo.name] gender:[weakSelf validateString:userinfo.gender] birthday:[weakSelf validateString:userinfo.birthday] phoneNum:[weakSelf validateString:userinfo.phoneNumber] school:[weakSelf validateString:userinfo.school] grade:[weakSelf validateString:userinfo.grade] clzss:[weakSelf validateString:userinfo.clzss] recent:[weakSelf validateString:userinfo.recent] topicId:@"" completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
                
                if (isSuccess) {
                    NSLog(@"修改成功");
                }
                
            }];
            
            NSMutableArray *array  = weakSelf.datas[indexPath.section];
            NSDictionary * dict = @{@"性别":[result isEqualToString:@"0"]?@"男":@"女"};
            [array replaceObjectAtIndex:indexPath.row withObject:dict];
            [weakSelf.tableView reloadData];
        };
        

        [self.navigationController pushViewController:sexVC animated:YES];
    }
    else if (indexPath.row == 3 && indexPath.section == 0)
    {
        [self edit:indexPath withEditTitle:@"学校" withEditValue:userinfo.school withPlaceholder:@""];
    }
    else if (indexPath.row == 4 && indexPath.section == 0)
    {
        [self edit:indexPath withEditTitle:@"年级" withEditValue:userinfo.grade withPlaceholder:@"请输入年级名字"];
    }
    else if (indexPath.row == 5 && indexPath.section == 0)
    {
        [self edit:indexPath withEditTitle:@"班级" withEditValue:userinfo.clzss withPlaceholder:@"请输入班级名字"];
    }
    else if (indexPath.row == 6 && indexPath.section == 0)
    {
        DDAddressVC *addressVC = [[DDAddressVC alloc] initWithNibName:@"DDAddressVC" bundle:nil];
        [self.navigationController pushViewController:addressVC animated:YES];
    }
    else if (indexPath.row == 7 && indexPath.section == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"丁当段位是用户获取积分、答题获得的评价等级独特算法计算所得，丁当段位高低反映了用户在丁当平台的贡献与能力的高低" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if (indexPath.row == 0 && indexPath.section == 1)
    {
        DDUpdatePasswordVC *updatePasswordVC = [[DDUpdatePasswordVC alloc] initWithNibName:@"DDUpdatePasswordVC" bundle:nil];
        [self.navigationController pushViewController:updatePasswordVC animated:YES];
    }
}

-(void) edit:(NSIndexPath *)indexPath withEditTitle:(NSString*)editTitle withEditValue:(NSString*)editValue withPlaceholder:(NSString *)placeholder
{
    __weak DDWoInfoVC *weakSelf = self;
    DDSharedUser *user = [DDSharedUser shared];
    DDUser *userinfo = user.userInfo;
    WoEditVC *editVC = [[WoEditVC alloc] initWithNibName:@"WoEditVC" bundle:nil];
    editVC.editTitle = editTitle;
    editVC.editValue = editValue;
    editVC.placeholderString = placeholder;
    editVC.resultBlock = ^(NSString *result){
        if ([editTitle isEqualToString:@"昵称"])
        {
            userinfo.nickName = result;
        }
        else if ([editTitle isEqualToString:@"学校"])
        {
            userinfo.school = result;
        }
        else if ([editTitle isEqualToString:@"班级"])
        {
            userinfo.clzss = result;
        }
        else if ([editTitle isEqualToString:@"年级"])
        {
            userinfo.grade = result;
        }
        DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
        [network userEdit:[weakSelf validateString:userinfo.userName] nickName:[weakSelf validateString:userinfo.nickName] name:[weakSelf validateString:userinfo.name] gender:[weakSelf validateString:userinfo.gender] birthday:[weakSelf validateString:userinfo.birthday] phoneNum:[weakSelf validateString:userinfo.phoneNumber] school:[weakSelf validateString:userinfo.school] grade:[weakSelf validateString:userinfo.grade] clzss:[weakSelf validateString:userinfo.clzss] recent:[weakSelf validateString:userinfo.recent] topicId:@"" completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
           
            if (isSuccess) {
                NSLog(@"修改成功");
            }
            
        }];
        
        NSMutableArray *array  = weakSelf.datas[indexPath.section];
        NSDictionary * dict = @{editTitle:result};
        [array replaceObjectAtIndex:indexPath.row withObject:dict];
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:editVC animated:YES];
}


-(NSString *)validateString:(NSString *)string
{
    return string == nil ? @"" : string;
}

#pragma mark - LXActionSheetDelegate
//- (void)lxactionSheet:(LXActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;{
//    if (buttonIndex == 0) {
//        [self takePhoto];
//    }else if (buttonIndex == 1){
//        [self LocalPhoto];
//    }
//}

////开始拍照
//-(void)takePhoto
//{
//    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
//    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
//    {
//        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//        picker.delegate = self;
//        //设置拍照后的图片可被编辑
//        picker.allowsEditing = NO;
//        picker.sourceType = sourceType;
//        [self presentViewController:picker animated:YES completion:nil];
//    }else
//    {
//        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
//    }
//}
//
////打开本地相册
//-(void)LocalPhoto
//{
//    //    _picker = [[UIImagePickerController alloc] init];
//    //    _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    //    _picker.delegate = self;
//    //    //设置选择后的图片可被编辑
//    //    _picker.allowsEditing = NO;
//    //    [self presentModalViewController:_picker animated:YES];
//    
//    ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc] init];
//    picker.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkTextColor],UITextAttributeTextColor,[UIFont boldSystemFontOfSize:18],UITextAttributeFont,@0.0,UITextAttributeTextShadowOffset, nil];
//    picker.maximumNumberOfSelection = 1;// 9 - _fileDatas.count;
//    picker.assetsFilter = [ALAssetsFilter allPhotos];
//    picker.showEmptyGroups=NO;
//    picker.delegate=self;
//    picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
//        if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
//            NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
//            return duration >= 5;
//        } else {
//            return YES;
//        }
//    }];
//    
//    [self presentViewController:picker animated:YES completion:NULL];
//    
//}
////当选择一张图片后进入这里
//-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//
//{
//    
//    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//    __weak DDWoInfoVC *weakSelf = self;
//    //当选择的类型是图片
//    if ([type isEqualToString:@"public.image"])
//    {
//        //先把图片转成NSData
//        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//        image = [image fixOrientation];
//        image = [image imageByScaleForSize:CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2)];
//        
//        
//        //todo写上传图片接口
//        DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
//        NSData *data = UIImageJPEGRepresentation(image, 1.0);
//        NSString *dataString = [data base64Encoding];
//        [network avatarSave:dataString completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
//            if (isSuccess) {
//                [weakSelf.headerImageView setImage:image];
//                 _imageInfo = [[ImageFileInfo alloc]initWithImage:image];
//                DDSharedUser *user = [DDSharedUser shared];
//    
//                [network userAvatar:user.userInfo.uid size:nil completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
//                    
//                    
//                    
//                }];
//                
//            }
//        }];
//
//        
//    }
//    
//    
//    [picker dismissViewControllerAnimated:YES completion:nil];
//    picker = nil;
//}
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    NSLog(@"您取消了选择图片");
//    [picker dismissViewControllerAnimated:YES completion:nil];
//    picker = nil;
//}
//#pragma mark  ZYQAssetPickerController Delegate
//-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
//    
//    __weak DDWoInfoVC *weakSelf = self;
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        //for (int i=0; i<assets.count; i++) {
//        ALAsset *asset=assets[0];
//        UIImage *image=[UIImage imageWithCGImage:asset.thumbnail];
//        //UIImage *image=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
//        //            image = [image imageByScaleForSize:CGSizeMake(self.view.frame.size.width * 1.5, self.view.frame.size.height * 1.5)];
//        
//        //            UIButton* button =  (UIButton*)[self.view viewWithTag:Tag_HeadButton];
//        //            [button removeFromSuperview];
//        //            [button setImage:image forState:UIControlStateNormal];
//        //            [self.view addSubview:button];
//        
//        
//        weakSelf.imageInfo = [[ImageFileInfo alloc]initWithImage:image];
//        
//               dispatch_async(dispatch_get_main_queue(), ^{
//            
//            [weakSelf.headerImageView setImage:image];
//            [picker dismissViewControllerAnimated:YES completion:nil];
//        });
//        
//        
//        //todo写上传图片接口
//        DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
//        NSData *data = UIImageJPEGRepresentation(image, 1.0);
//        NSString *dataString = [data base64Encoding];
//        [network avatarSave:dataString completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
//            if (isSuccess) {
//                [weakSelf.headerImageView setImage:image];
//                _imageInfo = [[ImageFileInfo alloc]initWithImage:image];
//                DDSharedUser *user = [DDSharedUser shared];
//                
//                [network userAvatar:user.userInfo.uid size:@"" completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
//                    
//                    user.userInfo.avatarUrl = [NSString stringWithFormat:@"http://dd5s.oss-cn-shenzhen.aliyuncs.com/upload/head/%@/m.jpg",user.userInfo.uid];
//                    
//                }];
//                
//            }
//        }];
//        
//        
//    });
//}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self openPhoto];
    }else if (buttonIndex == 1) {
        [self openCamera];
        
    }
}
- (void)openCamera {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }
}
- (void)openPhoto {
    
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

#pragma mark - picker dataSource & delegate
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return array_picker.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return array_picker[row];
}

#pragma mark - pickerImage Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    image = [image fixOrientation];
    image = [image imageByScaleForSize:CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2)];
    
    _paramImage_process = image;
    [picker dismissViewControllerAnimated:YES completion:^{
        [self openEditor:nil];
    }];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark -  PECropViewControllerDelegate
- (void)cropViewController:(PECropViewController *)controller didFinishCroppingImage:(UIImage *)croppedImage{
    [controller dismissViewControllerAnimated:YES completion:nil];
    _paramImage_process = croppedImage;
    
    

    
    
    
//    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
//    NSData *data = UIImageJPEGRepresentation(_paramImage_process, 0.1);
//    NSString *dataString = [data base64Encoding];
//    [network uploadImage:@{@"extName":@"jpg",@"data":dataString} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
//        
//        if (object) {
//            NSDictionary *dict = (NSDictionary *)object;
//            DDTagDB *tagDB = [[DDTagDB alloc] init];
//            tagDB.x = _x;
//            tagDB.y = _y;
//            tagDB.tagIndex = _index;
//            tagDB.type = _type;
//            tagDB.result = [dict objectForKey:@"data"];
//            tagDB.qid = _qid;
//            if (![tagDB saveToDB]) {
//                NSLog(@"插入TagDB数据库失败");
//            }
//            
//            if (_result) {
//                _result(self.voice.recordPath,_paramImage_process,_type);
//            }
//            
//            [self dismissViewControllerAnimated:YES completion:nil];
//        }else{
//            NSLog(@"上传失败");
//        }
//        
//    }];
    
    
    
        __weak DDWoInfoVC *weakSelf = self;
            //todo写上传图片接口
            DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
            NSData *data = UIImageJPEGRepresentation(_paramImage_process, 0.0);
            NSString *dataString = [data base64Encoding];
            [network avatarSave:dataString completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
                if (isSuccess) {
                    [weakSelf.headerImageView setImage:weakSelf.paramImage_process];
                     _imageInfo = [[ImageFileInfo alloc]initWithImage:weakSelf.paramImage_process];
                    DDSharedUser *user = [DDSharedUser shared];
                   // user.userInfo.avatarUrl = [NSString stringWithFormat:@"http://dd5s.oss-cn-shenzhen.aliyuncs.com/upload/head/%@/m.jpg",user.userInfo.uid];
                    
                    [network avatarGet:@[user.userInfo.uid?user.userInfo.uid:@""] size:nil completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
                        if (isSuccess) {
                            if (dict.count > 0) {
                                NSArray *d = (NSArray *)dict;
                                NSDictionary *userDict = (NSDictionary *)d[0];
                                    if (userDict.count > 0) {
                                        user.userInfo.avatarUrl = [userDict objectForKey:@"url"];
                                    }                               
                            }
                        }
                    
                   // [network userAvatar:user.userInfo.uid size:nil completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {

                        
                       // user.userInfo.avatarUrl = [NSString stringWithFormat:@"http://dd5s.oss-cn-shenzhen.aliyuncs.com/upload/head/%@/m.jpg",user.userInfo.uid];
                    }];
                    
                }
            }];
    
    
}

- (void)cropViewControllerDidCancel:(PECropViewController *)controller{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)openEditor:(id)sender
{
    PECropViewController *controller = [[PECropViewController alloc] init];
    controller.delegate = self;
    controller.image = _paramImage_process;
    
    UIImage *image = _paramImage_process;
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    CGFloat length = MIN(width, height);
    controller.imageCropRect = CGRectMake((width - length) / 2,
                                          (height - length) / 2,
                                          length,
                                          length);
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
    
    [self presentViewController:navigationController animated:YES completion:nil];
}


@end
